﻿@{
    //ViewBag.Title = "FinanceList";
    //Layout = "~/Views/Shared/_Layout.cshtml";
    Layout = null;
}

<script type="text/html" id="toolbarTablefinance">
    <div class="layui-btn-container" style="width: 1104px;">
        <div class="layui-form-item" style="position: relative; ">
            <label class="layui-form-label">日期</label>
            <div class="layui-input-inline">
                <input type="text" id="startDate" class="layui-input" placeholder="开始日期">
            </div>
            <div class="layui-input-inline">
                <input type="text" id="endDate" class="layui-input" placeholder="结束日期">
            </div>
            <div class="layui-input-inline">
                <input type="text" id="contractNumber" class="layui-input" placeholder="合同编号">
            </div>
            <div class="layui-input-inline">
                <select id="typeFilter" lay-filter="typeFilter">
                    <option value="">全部类型</option>
                    <option value="0">出账</option>
                    <option value="1">入账</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select id="verifyFilter" lay-filter="verifyFilter">
                    <option value="">全部状态</option>
                    <option value="1">已审核</option>
                    <option value="0">未审核</option>
                </select>
            </div>
            <button class="layui-btn layui-btn-sm" lay-event="add">出\入帐</button>
            <button class="layui-btn layui-btn-sm" lay-event="search">搜索</button>
        </div>
        
    </div>
</script>

<script type="text/html" id="toolbarColfinance">
    {{#  if(d.Verify == 1){ }}
    <button class="layui-btn layui-btn-xs layui-btn-disabled" disabled>资金确认</button>
    {{#  } else { }}
    <button class="layui-btn layui-btn-xs" lay-event="confirm">资金确认</button>
    {{#  } }}
</script>

<script type="text/html" id="tpltype">
    {{#  if(d.ServiceType == 0){ }}
    出账
    {{#  } else if(d.ServiceType == 1){ }}
    入账
    {{# } }}
</script>

<body>
    <div style="padding: 16px;">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-row">
                    <table class="layui-hide" id="testfinance" lay-filter="testfinance"></table>
                    <script>
                        var table;
                        layui.use(['table', 'util', 'form', 'layer', 'laydate'], function () {
                            var table = layui.table;
                            var util = layui.util;
                            var form = layui.form;
                            var layer = layui.layer;
                            var laydate = layui.laydate;


                            // 初始化日期选择器
                            laydate.render({
                                elem: '#startDate',
                                format: 'yyyy-MM-dd'
                            });

                            laydate.render({
                                elem: '#endDate',
                                format: 'yyyy-MM-dd'
                            });

                            // 渲染表格
                            table.render({
                                elem: '#testfinance',
                                url: '/Finance/DataList',
                                toolbar: '#toolbarTablefinance',
                                defaultToolbar: [],
                                height: 'full-200',
                                cellMinWidth: 90,
                                page: true,
                                limits: [5, 10, 15, 20],
                                limit: 10,
                                cols: [[
                                    { type: 'checkbox', fixed: 'left' },
                                    {
                                        field: 'CreateTime',
                                        title: '创建时间',
                                        templet: function (d) {
                                            const regex = /\/Date\((\d+)\)\//;
                                            const match = d.CreateTime.match(regex);
                                            if (match) {
                                                return util.toDateString(parseInt(match[1]), 'yyyy-MM-dd HH:mm:ss');
                                            }
                                            return d.CreateTime;
                                        }
                                    },
                                    {
                                        field: 'Type',
                                        title: '类型',
                                        templet: function (d) {
                                            return d.Type === 0 ? '出账' : '入账';
                                        }
                                    },

                                    { field: 'EntryPrice', title: '金额' },
                                    { field: 'PackageName', title: '款项名称' },
                                    { field: 'EntryPeopleName', title: '入账人' },
                                    { field: 'FounderName', title: '创建人' },
                                    { field: 'VerifyName', title: '审核人' },
                                    {
                                        field: 'Verify',
                                        title: '状态',
                                        templet: function (d) {
                                            return d.Verify === 1 ? '已审核' : '未审核';
                                        }
                                    },
                                    { field: 'ContractNumber', title: '合同编号' },
                                    {
                                        title: '操作',
                                        fixed: 'right',
                                        toolbar: '#toolbarColfinance',
                                        width: 100
                                    },
                                    { field: 'ParturientName', title: '孕妇名' }
                                ]],

                                parseData: function (res) {
                                    return {
                                        "code": res.code,
                                        "msg": res.msg,
                                        "count": res.count,
                                        "data": res.data
                                    };
                                }
                            });

                            // 工具栏事件
                            table.on('toolbar(testfinance)', function (obj) {
                                switch (obj.event) {
                                    case 'search': // 搜索按钮点击事件
                                        var startDate = $('#startDate').val();
                                        var endDate = $('#endDate').val();
                                        var type = $('#typeFilter').val();  //类型搜索
                                        var verify = $('#verifyFilter').val(); //状态
                                        var contractNumber = $('#contractNumber').val();  // 修正这里的选择器，添加#符号

                                        // 重新加载表格数据，应用日期过滤
                                        table.reload('testfinance', {
                                            where: {
                                                startDate: startDate,
                                                endDate: endDate,
                                                type: type,
                                                verify: verify,
                                                contractNumber: contractNumber
                                            },
                                            page: {
                                                curr: 1 // 从第一页开始
                                            }
                                        });
                                        break;
                                    case 'add':
                                        // 添加按钮逻辑
                                        if (obj.event == 'add') {
                                            layer.open({
                                                type: 2,
                                                content: '/Finance/Detail',
                                                area: ['60%', '80%']
                                            });
                                        }
                                        break;
                                }
                            });

                            // 行工具事件 - 资金确认
                            table.on('tool(testfinance)', function (obj) {
                                var data = obj.data;
                                console.log(data);
                                var layEvent = obj.event;

                                if (layEvent === 'confirm') {
                                    layer.confirm('确定要审核该记录吗？', { icon: 3, title: '提示' }, function (index) {
                                        $.ajax({
                                            url: '/Finance/ConfirmFinance',
                                            type: 'POST',
                                            data: { id: data.Id },
                                            success: function (res) {
                                                if (res.code === 0) {
                                                    layer.msg('审核成功', { icon: 1 });
                                                    table.reload('testfinance');
                                                } else {
                                                    layer.msg(res.msg || '审核失败', { icon: 2 });
                                                }
                                            },
                                            error: function () {
                                                layer.msg('请求失败', { icon: 2 });
                                            }
                                        });
                                        layer.close(index);
                                    });
                                }
                            });

                            // 初始化表单元素
                            form.render();
                        });


                    </script>
                </div>
            </div>
        </div>
    </div>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
            <button class="layui-btn layui-btn-sm" lay-event="getData">获取当前页数据</button>
            <button class="layui-btn layui-btn-sm" id="dropdownButton">
                下拉按钮
                <i class="layui-icon layui-icon-down layui-font-12"></i>
            </button>
            <button class="layui-btn layui-btn-sm" id="reloadTest">
                重载测试
                <i class="layui-icon layui-icon-down layui-font-12"></i>
            </button>
            <button class="layui-btn layui-btn-sm layui-btn-primary" id="rowMode">
                <span>{{= d.lineStyle? '多行' : '单行' }}模式</span>
                <i class="layui-icon layui-icon-down layui-font-12"></i>
            </button>
        </div>
    </script>
    <script type="text/html" id="barDemo">
        <div class="layui-clear-space">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs" lay-event="more">
                更多
                <i class="layui-icon layui-icon-down"></i>
            </a>
        </div>
    </script>
</body>